﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
//using Props = SharpMap.Wpf.Properties;
namespace SharpMap.Wpf
{
    public class MapTask
    {

        public event EventHandler Completed;

        public event EventHandler Deactivated;

        public CommandBindingCollection CommandBindings
        {
            get
            {
                if (this._commandBindings == null)
                {
                    this._commandBindings = new CommandBindingCollection();
                }
                return this._commandBindings;
            }
        }

        public Cursor Cursor
        {
            get
            {
                return this._cursor;
            }
            set
            {
                this._cursor = value;
            }
        }

        public InputBindingCollection InputBindings
        {
            get
            {
                if (this._inputBindings == null)
                {
                    this._inputBindings = new InputBindingCollection();
                }
                return this._inputBindings;
            }
        }

        public void Activate(TaskContext context)
       {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (this._activeTool != null)
            {
                throw new InvalidOperationException("TaskAlreadyActivated");
            }
            MapTool tool = context.ActiveTool;
            tool.SetActiveTask(this);
            this._activeTool = tool;
       }

        public void Complete()
        {
            if (this._activeTool == null)
            {
                throw new InvalidOperationException("ObjectNotActive");
            }
            
            try
            {
                this.OnCompleted(EventArgs.Empty);
              }
            finally
            {
                this._activeTool.ClearActiveTask();
                this._activeTool = null;
                this.OnDeactivated(EventArgs.Empty);
            }
        }

        protected virtual void OnCompleted(EventArgs e)
        {
            EventHandler handler = Completed;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnDeactivated(EventArgs e)
        {
            EventHandler handler = Deactivated;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public bool IsActive
        {
            get
            {
                return (this._activeTool != null);
            }
        }

        CommandBindingCollection _commandBindings;
        Cursor _cursor;
        InputBindingCollection _inputBindings;
        MapTool _activeTool;
    }
}
